/**
 * 
 */
package cc.rico.tutorial.topic;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import cc.rico.tutorial.RabbitMQConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import lombok.extern.slf4j.Slf4j;


/**
 * @author rico
 *
 */
@Slf4j
public class EmitLogTopic {
	private static final String EXCHANGE_NAME = "topicLogs";
	private static final String EXCHANGE_TYPE= "topic";

	/**
	 * @param args
	 * @throws IOException 
	 * @throws TimeoutException 
	 */
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection conn = RabbitMQConnectionUtil.getConnection();
		 
		EmitLogTopic.emitLog(conn, "file.log", "messge for file.");
		EmitLogTopic.emitLog(conn, "console.log", "message for console");

		RabbitMQConnectionUtil.close(conn);
	}
	
	private static void emitLog(Connection conn, String routingKey, String message) throws IOException, TimeoutException {
		Channel channel = conn.createChannel();
		
		channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE);
		channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
		System.out.println("[type = "+EXCHANGE_TYPE+" ]Sent " + message);

		RabbitMQConnectionUtil.close(channel);
	}

}
